package com.app.hanoitips.travel.custlistview;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.app.hanoitips.travel.R;
//import com.app.hanoitips.travel.R.id;
//import com.app.hanoitips.travel.R.layout;









import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
 
public class CustomizedListView extends Activity implements View.OnClickListener{
    // All static variables
    String URL = "";
    // XML node keys
    static final String KEY_SHOP = "shop"; // parent node
    static final String KEY_NAME = "name";
    static final String KEY_URL = "url";
    static final String KEY_THUMB_URL = "thumb_url";
 
    ListView list;
    CustomeAdapter adapter;
    String type;
    String fileInput;
    Button bt1,bt2,bBack,btClear;
    EditText search;
    Class<?> ourClass;
    int choice;
    SharedPreferences sp;
    String btText1,btText2,btText1_1,btText2_1;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listview_menu);
        Intent intent=getIntent();
        Bundle extra=intent.getExtras();
        fileInput=extra.getString("key");
        InputStream input;
        AssetManager getAssets=getAssets();
  	  	try {
            input = getAssets.open(fileInput+".xml");

             int size = input.available();
             byte[] buffer = new byte[size];
             input.read(buffer);
             input.close();

             // byte buffer into a string
             String text = new String(buffer);
             
             URL=text;
  	  		} catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
  	  		}
        ArrayList<HashMap<String, String>> arrList1 = new ArrayList<HashMap<String, String>>();
        //ArrayList<HashMap<String, String>> arrList2 = new ArrayList<HashMap<String, String>>();
 
        XMLParser parser = new XMLParser();
       //String xml = parser.getXmlFromUrl(URL); // getting XML from URL
        //Document doc = parser.getDomElement(xml); // getting DOM element
        Document doc=parser.getDomElement(URL) ;
        NodeList nl = doc.getElementsByTagName(KEY_SHOP);
        // looping through all song nodes &lt;song&gt;
        for (int i = 0; i < nl.getLength(); i++) {
            // creating new HashMap
            HashMap<String, String> map = new HashMap<String, String>();
            Element e = (Element) nl.item(i);
            map.put(KEY_NAME, parser.getValue(e, KEY_NAME));
            map.put(KEY_URL, parser.getValue(e, KEY_URL));
            map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));
 
            // adding HashList to ArrayList
            arrList1.add(map);
        }
		sp=PreferenceManager.getDefaultSharedPreferences(this);
		choice=sp.getInt("keyLang", 0);
 
        list=(ListView)findViewById(R.id.list);
        bt1=(Button) findViewById(R.id.bChangeType1);
        bt2=(Button) findViewById(R.id.bChangeType2);
        bBack=(Button) findViewById(R.id.btBackward);
        btClear=(Button) findViewById(R.id.btCancel);
        btClear.setOnClickListener(this);
        if(choice==0){
        	btText1="Vietnam";
        	btText2="Japan";
        	btText1_1="Vietnam Style";
        	btText2_1="Western Style";
        }else if(choice==1){
        	btText1="Việt Nam";
        	btText2="Nhật Bản";
        	btText1_1="Kiểu Việt";
        	btText2_1="Kiểu Âu";
        }else{
        	btText1="ベトナム";
        	btText2="日本";
        	btText1_1="ベトナム ";
        	btText2_1="西部";
        }
        // Getting adapter by passing xml data ArrayList
        if(fileInput.equalsIgnoreCase("vnRestaurants") || fileInput.equalsIgnoreCase("jRestaurants")){
        		bt1.setText(btText1);
        		bt1.setOnClickListener(this);
        		bt2.setText(btText2);
        		bt2.setOnClickListener(this);
					
        		adapter=new CustomeAdapter(this, arrList1);
        }else if(fileInput.equalsIgnoreCase("vnCafes") || fileInput.equalsIgnoreCase("westCafes")){
        	bt1.setText(btText1_1);
        	bt1.setOnClickListener(this);
    		bt2.setText(btText2_1);
    		bt2.setOnClickListener(this);
    		    adapter=new CustomeAdapter(this, arrList1);	
        }else if(fileInput.equalsIgnoreCase("teas")){
        	bt1.setEnabled(false);
        	bt2.setEnabled(false);
        	bt1.setVisibility(View.INVISIBLE);
        	bt2.setVisibility(View.INVISIBLE);
        	    adapter= new CustomeAdapter(this, arrList1);
        }else if(fileInput.equalsIgnoreCase("taxi")){
        	bt1.setEnabled(false);
        	bt2.setEnabled(false);
        	bt1.setVisibility(View.INVISIBLE);
        	bt2.setVisibility(View.INVISIBLE);
        		adapter=new CustomeAdapter(this, arrList1);
        }
        search=(EditText) findViewById(R.id.etSearch);
        search.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub
				CustomizedListView.this.adapter.getFilter().filter(s.toString());
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub
				
				
			}
			
			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
				
			}
		});
        list.setAdapter(adapter);
        bBack.setOnClickListener(this);
        // Click event for single list row
        list.setOnItemClickListener(new OnItemClickListener() {
 
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
            	//Class ourClass=Class.forName("com.app.hanoitips.travel.DisplayDataTemp");
            	try {
            		TextView txtName=(TextView)view.findViewById(R.id.tvShopName);
                	String name=txtName.getText().toString();
               		ourClass=Class.forName("com.app.hanoitips.travel.DisplayDataTemp");
            		Intent ourIntent=new Intent(CustomizedListView.this,ourClass);
            		//ourIntent.putExtra("key", name);
            		Bundle extras=new Bundle();
            		extras.putString("key", name);
            		extras.putString("url", fileInput);
            		ourIntent.putExtras(extras);
            		startActivity(ourIntent);
				} catch (Exception e) {
					// TODO: handle exception
					e.printStackTrace();
				}
            	
            }
        });
    }
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId()){
		case R.id.bChangeType1:
			Intent vIntent=new Intent("android.intent.action.CustomizedListView");
			Bundle vExtra=new Bundle();
			String bt1_Text=bt1.getText().toString();
			if(bt1_Text.equalsIgnoreCase("Vietnam") || bt1_Text.equalsIgnoreCase("Việt Nam") || bt1_Text.equalsIgnoreCase("ベトナム")){
			fileInput="vnRestaurants";
			}
			if(bt1_Text.equalsIgnoreCase("Vietnam Style") || bt1_Text.equalsIgnoreCase("Kiểu Việt") || bt1_Text.equalsIgnoreCase("ベトナム")){
			fileInput="vnCafes";	
			}
			vExtra.putString("key", fileInput);
			vIntent.putExtras(vExtra);
			startActivity(vIntent);
			break;
		case R.id.bChangeType2:
			Intent jIntent=new Intent("android.intent.action.CustomizedListView");
			Bundle jExtra=new Bundle();
			String bt2_Text=bt2.getText().toString();
			if(bt2_Text.equalsIgnoreCase("Japan") || bt2_Text.equalsIgnoreCase("Nhật Bản") || bt2_Text.equalsIgnoreCase("日本")){
				fileInput="jRestaurants";
				}
			if(bt2_Text.equalsIgnoreCase("Western Style") || bt2_Text.equalsIgnoreCase("Kiểu Âu") || bt2_Text.equalsIgnoreCase("西部")){
				fileInput="westCafes";	
				}
			jExtra.putString("key", fileInput);
			jIntent.putExtras(jExtra);
			startActivity(jIntent);
			break;
		case R.id.btCancel:
			search.setText("");
			search.setFocusable(true);
		case R.id.btBackward:
			finish();
			break;
		}
	}
}
